<?php get_header(); ?>

<?php
global $authordata;
$aOptions       = SofaSuppaStore::initOptions( false );
$allow_comments = $aOptions[ 'allow_comments' ];
$trim_summary   = ( strlen( $aOptions[ 'trim_summary' ] ) > 0 ) ? stripslashes( $aOptions[ 'trim_summary' ] ) : 25;

// custom templates
$narrow_grided_page   = intval( get_option( 'welcomenarrowgrid' ) );
$narrow_scrolled_page = intval( get_option( 'welcomenarrow' ) );
$wide_grided_page     = intval( get_option( 'welcomefull' ) );
$common_blog_page     = intval( get_option( 'commonblog' ) );
?>

<!-- main content start -->

<div class="grid_8">
    
    <div class="grid_8 clearfix alpha omega">
    
    	<!-- title start -->
        
        <h1><?php _e( "Search results for:", "sofa_suppastore" ); ?> "<?php echo $_GET[ 's' ]; ?>"</h1>
        
        <!-- title end -->
        
        <?php if( $wp_query->found_posts > 0 ) { ?>
        
        <p class="spaced">
        
        <!-- search filtering buttons start -->
        
        <span class="sfspan"><?php _e( "Narrow down serch results?", "sofa_suppastore" ); ?></span>
        <span class="srchfilter"><a href="<?php echo home_url() . '/?s=' . $_GET[ 's' ] . '&amp;srchtype=product'; ?>" rel="nofollow">
        <?php _e( "Products Only", "sofa_suppastore" ); ?>
        </a></span>
        <span class="srchfilter">
        <a href="<?php echo home_url() . '/?s=' . $_GET[ 's' ] . '&amp;srchtype=else'; ?>" rel="nofollow">
        <?php _e( "All but Products", "sofa_suppastore" ); ?>
        </a></span>
        <span class="srchfilter">
        <a href="<?php echo home_url() . '/?s=' . $_GET[ 's' ]; ?>" rel="nofollow">
        <?php _e( "Everything", "sofa_suppastore" ); ?>
        </a></span>
        
        <!-- search filtering buttons end -->
        
        </p>
        
        <?php } ?>
        
    </div>
    
	<?php
	// use different queries for different search filtering
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    if( isset( $_GET[ 'srchtype' ] ) && stripslashes( strip_tags( $_GET[ 'srchtype' ] ) ) == 'product' ) { // handle products
        $args = array_merge( $wp_query->query, array( 'post_type' => array( 'product' ), 'paged' => $paged ) );
    } elseif( isset( $_GET[ 'srchtype' ] ) && stripslashes( strip_tags( $_GET[ 'srchtype' ] ) ) == 'else' ) { // handle everything else
        $args = array_merge( $wp_query->query, array( 'post_type' => array( 'post', 'page' ), 'paged' => $paged, 'post__not_in' => array( $narrow_grided_page, $narrow_scrolled_page, $wide_grided_page, $common_blog_page ) ) );
    } else $args = array_merge( $wp_query->query, array( 'post_type' => array( 'post', 'page', 'product' ), 'paged' => $paged, 'post__not_in' => array( $narrow_grided_page, $narrow_scrolled_page, $wide_grided_page, $common_blog_page ) ) ); // default
    ?>

	<?php 
	// start custom wordpress loop
	query_posts( $args );
	if( have_posts() ) : while( have_posts() ) : the_post(); ?>
    
    <div class="castshadow grid_8 clearfix alpha omega">
    
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
    <?php if( $post->post_type == 'post' ) { ?>
    
        <span class="sidenewsinfo small clearfix">
        <?php the_time( get_option( 'date_format' ) ); ?> 
        &bull; 
        <?php echo sprintf( __( 'Posted by <a href="%s">%s</a> in', 'sofa_suppastore' ), get_author_posts_url( $authordata->ID, $authordata->user_nicename ), get_the_author() ); ?> <?php the_category( ', ' ); ?>
        &bull; 
        <?php the_tags(); ?>
        &bull; 
        <?php comments_popup_link( '0 ' . __( "comments", "sofa_suppastore" ), '1 ' . __( "comment", "sofa_suppastore" ), '% ' . __( "comments", "sofa_suppastore" ) ); ?>
        </span>
    
    <?php } ?>
    
    <?php
	$thumb_data = '';
	$c_post_id = intval( get_the_ID() );
	$c_post_perma = get_permalink( $c_post_id );
	/* extract image - if any */
	$attImagez = array();
	$attImagez =& get_children( 'post_type=attachment&post_mime_type=image&orderby=menu_order&order=ASC&post_parent=' . $c_post_id );
	$arrIM = array_keys( $attImagez );
	if( $arrIM[ 0 ] ) {
		$targ_img = wp_get_attachment_image_src( $arrIM[ 0 ], 'thumbnail' );
		$thumb_data .= '<a href="' . $c_post_perma . '">';
		$thumb_data .= '<img src="' . $targ_img[ 0 ] . '" class="size-thumbnail alignleft" alt="" />';
		$thumb_data .= '</a>';
	}
	echo '<p>' . $thumb_data . trim_the_content( get_the_excerpt(), __( "...READ MORE", "sofa_suppastore" ), $c_post_perma, (int)$trim_summary ) . '</p>';
    ?>
    
    </div>

	<?php endwhile; // end custom wordpress loop ?>
    
    <!-- pagination start -->

	<?php
	get_template_part( 'wp-pagenavi' );
	if( function_exists( 'wp_page_numbers' ) ) {
		wp_page_numbers();
	}
	elseif( function_exists( 'wp_pagenavi' ) ) {
		wp_pagenavi();
	} else {
		global $wp_query;
		$total_pages = $wp_query->max_num_pages;
		if ( $total_pages > 1 ) {
			echo '<div id="pagination" class="wp-pagenavi grid_8 alpha omega"><span class="nava">';
				posts_nav_link( ' ', __( '&laquo; Previous page', 'sofa_suppastore' ), __( 'Next page &raquo;', 'sofa_suppastore' ) );
			echo '</span></div>';
		}
	}
    ?>
    
    <!-- pagination end -->
    
    <?php else : ?>
    
    <!-- no content start -->

	<div class="grid_8 clearfix alpha omega">
    
        <h1><?php _e( "Nothing found!", "sofa_suppastore" ); ?></h1>
        <p><?php _e( "Sorry but no posts found. Take your time, there's gonna be soon to see something listed here.", "sofa_suppastore" ); ?></p>
    
    </div>
    
    <!-- no content end -->

	<?php endif; ?>
    
</div>

<!-- main content end -->

<!-- include sidebar start -->

    <?php get_template_part( 'sidebar_search' ); ?>
    
<!-- include sidebar end -->

<?php get_footer(); ?>